package com.jike.jkgame.network;

import java.util.Timer;
import java.util.TimerTask;

public class HeartbeatTask {
	
	private static final long HEARTBEAT_INTERVAL = 1000; // 1 seccond
	private Timer mTimer;
	private TimerTask mTask;
	private boolean mBeating;
	
	private static HeartbeatTask mInstance;
	
	private HeartbeatTask(){
		mTimer = new Timer(true);
		mTask = new HeartbeatTimerTask();
		mBeating = false;
	}
	
	public HeartbeatTask get() {
		if (mInstance == null) {
			mInstance = new HeartbeatTask();
		}
		
		return mInstance;
	}
	
	public void start() {
		if (!mBeating) {
			mTimer.schedule(mTask, 0, HEARTBEAT_INTERVAL);
			mBeating = true;
		}
	}
	
	public void stop() {
		mTimer.cancel();
		mTimer.purge();
		mBeating = false;
	}
	
	private class HeartbeatTimerTask extends TimerTask {
		@Override
		public void run() {
			// TODO 发送心跳包
			
		}
	}

}
